package io.gitee.chearnee.fw.common.utils;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 11/24/2022 3:57 PM
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MapUtil {


    public static Object getDeepObject(Map<String, Object> map, String keyConcatString) {
        if (!StringUtils.hasText(keyConcatString)) {
            return null;
        }
        String[] keys = keyConcatString.split(ConstStringPool.SPLIT_DOT);
        Iterator<String> keysIterator = Arrays.stream(keys).iterator();
        return getMapValue(keysIterator, map);
    }

    private static Object getMapValue(Iterator<String> keysIterator, Map<String, Object> map) {
        if (keysIterator.hasNext()) {
            String nextKey = keysIterator.next();
            Object o = map.get(nextKey);
            if (o instanceof Map) {
                return getMapValue(keysIterator, (Map<String, Object>) o);
            }
            return o;
        }
        return map;
    }
}
